import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
import serial_deal
import threading

ser=serial_deal.ck_deal(key=1)
def SAVE_AND_OPEN():
    if ser.key==1:
        ser.open_port(AT_Entry.get(),int(BPS_Entry.get()),5)
        if ser.key==0:
            AT_Entry.config(state='disable')
            BPS_Entry.config(state='disable')
            runT=threading.Thread(target=READ_DATA_scr)
            runT.start()
    else:
        ser.close_port()
        AT_Entry.config(state='able')
        BPS_Entry.config(state='able')
def SEND_DATA():
    ser.send_data(AT_Entry_SEND.get())
def READ_DATA_scr():
    while True:
        try:
            scr_TEXT.insert(tk.END,ser.read_data())
            scr_TEXT.see(tk.END)
        except:
            break


win=tk.Tk()
win.title('串口调试工具demo')
# win.iconbitmap(r'D:\pppython\python\old\串口调试工具\1.ico')
# win.geometry('400x300')
win.resizable(0,0)


AT_Lable=ttk.Label(win,text='AT')
AT_Lable.grid(column=0,row=0,stick='WE')
AT_Entry=ttk.Entry(win)
AT_Entry.grid(column=1,row=0,stick='WE')
BPS_Label=ttk.Label(win,text='BPS')
BPS_Label.grid(column=2,row=0,stick='WE')
BPS_Entry=ttk.Entry(win)
BPS_Entry.grid(column=3,row=0)
AT_SAVE=ttk.Button(win,text='SAVE',command=SAVE_AND_OPEN)
AT_SAVE.grid(column=4,row=0,stick='WE')

AT_Entry_SEND=ttk.Entry(win)
AT_Entry_SEND.grid(column=0,columnspan=4,row=1,stick='WE')
AT_SEND_Button=ttk.Button(win,text='SEND',command=SEND_DATA)
AT_SEND_Button.grid(column=4,row=1,stick='WE')

scr_TEXT=scrolledtext.ScrolledText(win,width=30)
scr_TEXT.grid(column=0,columnspan=5,row=2,stick='WE')



win.mainloop()